package org.odk.clinic.android.activities;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.odk.clinic.android.R;
import org.odk.clinic.android.R.id;
import org.odk.clinic.android.database.SQLiteAdapter;
import org.odk.clinic.android.openmrs1.encounter;
import org.odk.clinic.android.openmrs1.encounter_type;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class NewEncounterActivity1 extends Activity {
	private Button mPrevious,msave;
	private EditText mdate,mpatient,mdesc;
	private Spinner spinencountertype,spinprovider;
	int mYear;
	int mMonth;
	int mDay;
	//
	private SQLiteAdapter mDB;
	private Cursor mCursor;
	java.text.DateFormat date = new SimpleDateFormat("dd-mm-yyyy");
	//
	private encounter en;
	private encounter_type enty;
	//
	private int providerid;
	
	protected void onCreate(Bundle ici){
		super.onCreate(ici);
		setContentView(R.layout.add_encounter_1);
		setTitle(getString(R.string.app_name) + ">" + getString(R.string.add_encounter1));
		//spinner provider
		String[] items = new String[]{"Choose... ","Unknown","Supper User "};
		spinprovider = (Spinner)this.findViewById(R.id.spinprovider);
		spinprovider.setAdapter(new ArrayAdapter<String>(getApplicationContext(),android.R.layout.select_dialog_item,items));
		spinprovider.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
	    	@Override 
	    	public void onItemSelected(AdapterView<?> parent,View view,int pos, long i){
	    		 providerid = (int)parent.getItemIdAtPosition(pos);
	    		
	    	}
	    	@Override
	    	public void onNothingSelected(AdapterView <?>parent){
	    		
	    	}
	    	
		});
		//spinner encounter type
		spinencountertype = (Spinner)this.findViewById(R.id.spinerenountertype);
		ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.encouter_type, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinencountertype.setAdapter(adapter);
		//set date current
		final Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);
		mdate = (EditText)this.findViewById(R.id.encountedate);
		mdate.setText(new StringBuilder()
		.append(mDay).append("-")
		.append(mMonth).append("-")
		.append(mYear)
				
		);
		//
		mDB = new SQLiteAdapter(this);
		mDB.open();
		
		////
		
		
		//
	//	mpatient =(EditText)this.findViewById(R.id.patientid);
		mdesc = (EditText)this.findViewById(R.id.desc);
		msave = (Button)this.findViewById(R.id.btnencountersave);
		msave.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int mid = ListPatientActivity2.mid;
				String patient = null;
				mCursor = mDB.getPerson(mid);
				if(mCursor.moveToFirst()){
					patient = mCursor.getString(mCursor.getColumnIndex(SQLiteAdapter.KEY_GIVENNAME));
					int patient_id = mDB.find_patien_id(patient);
					
					en = new encounter();enty = new encounter_type();
					
					enty.setEncounterName(spinencountertype.getSelectedItem().toString());
					enty.setEncounterDesc(mdesc.getText().toString());
					
					try {
					en.setEncounterDate(date.parse(mdate.getText().toString()));
					}
					catch(ParseException e ){
						e.printStackTrace();
					}
					
					en.setPatientId(patient_id);
					en.setProviderId(patient_id);
					mDB.createEncounter(enty,en);
					setResult(RESULT_OK);
					finish();
				}
			}
		});
		
	}


}
